package cn.itsource.pet.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.service.ISearchMasterMsgService;
import cn.itsource.user.domain.LoginInfo;
import cn.itsource.user.domain.User;
import cn.itsource.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;

    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;




        /*  --searchMasterMsg/publish
        address	"云南省昆明市昆明市"
        age	0
        coat_color	""
        gender	1
        name	""
        pet_type	null
        price	0
        resources	""
        title	""
         */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            //获取token
            String token = request.getHeader("TOKEN");
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据id 查询用户
            User user =  userService.loadByLoginInfoId(loginInfo.getId());
            //添加 发布用户
            searchMasterMsg.setUser_id(user.getId());

            seachMasterMsgService.publish(searchMasterMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }

        return AjaxResult.me();
    }
}
